package org.eclipse.gittab.fretboard.draw2d.painters;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gittab.fretboard.Fretboard.FretboardListener;
import org.eclipse.gittab.fretboard.draw2d.figures.FretFigure;
import org.eclipse.gittab.fretboard.draw2d.figures.ModernFretInlayFigure;
import org.eclipse.gittab.fretboard.draw2d.figures.ModernFretInlayFigure.FretInlay;

public class LeftyFretboardPainter extends FretboardPainter {

	private int fretsTotal;
	private double fretsWidthTotal;

	public LeftyFretboardPainter(FretboardListener listener) {
		super(listener);
	}

	@Override
	protected double[] calcWidthOfFrets(int fretsTotal) {

		this.fretsTotal = fretsTotal;

		double incr = (WIDTH_LOW_FRET - WIDTH_HIGH_FRET) / fretsTotal;

		double[] fretsWidth = new double[fretsTotal];

		double width = WIDTH_HIGH_FRET;

		for (int i = 0; i < fretsWidth.length; i++) {
			fretsWidth[i] = width;
			width = width + incr;

			fretsWidthTotal = fretsWidthTotal + fretsWidth[i];
		}

		return fretsWidth;
	}

	@Override
	protected int getFretsPos() {

		return BORDER_LEFT;
	}

	@Override
	protected int getTuningPos() {

		// round up
		return BORDER_LEFT + (int) Math.ceil(fretsWidthTotal);
	}

	/**
	 * Due the {@link FretboardPainter} draws from left to right we have to
	 * calculate the fret index.
	 */
	@Override
	protected int getFretIndexByNr(int fretNr) {
		return fretsTotal - fretNr;
	}

	@Override
	protected FretInlay getModernFretInlay(FretFigure fret) {

		final Rectangle fretBounds = fret.getBounds();

		return new ModernFretInlayFigure.FretInlay() {

			@Override
			public PointList getPointList() {

				PointList list = new PointList();

				int x = fretBounds.x;
				int y = fretBounds.y;
				int w = fretBounds.width;
				int h = fretBounds.height;

				list.addPoint(new Point(x + w - SPACE_X, y + SPACE_Y));
				list.addPoint(new Point(x + w - SPACE_X, y + h - SPACE_Y));
				list.addPoint(new Point(x + SPACE_X, y + h - SPACE_Y - APT));
				list.addPoint(new Point(x + SPACE_X, y + SPACE_Y + APT));

				return list;
			}
		};
	}
}
